/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package src.services.customer;

/**
 *
 * @author jay
 */

import java.util.ArrayList;
import java.util.Iterator;
import src.domain.customer.Village;
import java.util.List;
import java.util.Map;
import javax.faces.model.SelectItem;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import src.HibernateUtil;
import src.domain.customer.Subdistrict;
import src.services.CRUEDService;

public class VillageModel implements java.io.Serializable {
    
    private CRUEDService service;
    
    public VillageModel() {
        service = new CRUEDService();
    }
    
    public List<Village> findAll(Map<String, String> filters) {
        return (List<Village>) service.findAll("Village", filters);
    }
    
    public List<SelectItem> listAll() {
        return service.listAll("Village", "villageId", "villageName");
    }
    
    public List<SelectItem> listAll(Map<String, String> filters) {
        //ini utk kebutuhan combo change aja
         return service.listAll("src.domain.customer.Village as tb inner join tb.subdistrict as tc", "villageId", "villageName",filters);
    }
    
    public List findAllCustom(Map<String, String> filters) {
        List result = new ArrayList();
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        org.hibernate.Transaction tx = session.beginTransaction();
              
        try {
            
            String where = "";
            if (filters!=null) {
                for (Map.Entry<String, String> e : filters.entrySet())
                    where += (where.length()>0 ? " AND " : "") + " lower(tb." + e.getKey() + ") like lower('%" + e.getValue() + "%')";
            }
            String sql =    "SELECT v.villageId, v.villageName, v.subdistrict.subdistrictId, s.subdistrictName " +
                            "FROM Village AS v, Subdistrict AS s "+
                            "WHERE v.subdistrict.subdistrictId=s.subdistrictId" + where;
            //Query q = session.createQuery ("from Village as tb "+ (where.length() > 0 ? " where " +  where : ""));
            Query q = session.createQuery (sql);
            //List<Object[]> = (List) q.list();
            List<Object[]> ls = (List) q.list();
            
            for (Object[] obj : ls) {
              Village data = new Village();
              data.setVillageId((Integer) obj[0]);
              data.setVillageName((String) obj[1]);
              data.setSubdistrict(new Subdistrict((Integer) obj[0], (String) obj[1]));
              result.add(data);
            }
              
            
            tx.commit();
            
        } catch (RuntimeException e) {
            
            if (tx != null && tx.isActive()) {
                try {tx.rollback(); } 
                catch (HibernateException e1) {}
                throw e;
            }
        }
        
        finally { }
        
        return result;
    }
    
    public boolean saveData(Village data) {
        return service.saveData(data);
    }
    
    public boolean deleteData(Village data) {
        return service.deleteData(data);
    }
}
